﻿using Bogus;
using Bookify.Application.Abstractions.Data;
using Bookify.Domain.Apartments;
using Dapper;
using Microsoft.AspNetCore.Server.Kestrel.Core.Features;

namespace Bookify.Api.Extensions
{
    public static class SeedDataExtensions
    {
        public static void SeedData(this IApplicationBuilder app)
        {
            using var scope = app.ApplicationServices.CreateScope();

            var sqlConnectionFactory = scope.ServiceProvider.GetRequiredService<ISqlConnectionFactory>();
            using var connection = sqlConnectionFactory.CreateConnection();

            bool exists = connection.QuerySingleOrDefault<bool>("SELECT EXISTS(SELECT 1 FROM public.apartments);");
            if (exists) 
            {
                var faker = new Faker();

                List<object> apartments = new();
                for (int i = 0; i < 100; i++)
                {
                    apartments.Add(new
                    {
                        Id = Guid.NewGuid(),
                        Name = faker.Company.CompanyName(),
                        Description = "Amazing view",
                        Country = faker.Address.Country(),
                        State = faker.Address.State(),
                        ZipCode = faker.Address.ZipCode(),
                        City = faker.Address.City(),
                        Street = faker.Address.StreetAddress(),
                        PriceAmount = faker.Random.Decimal(25, 200),
                        PriceCurrency = "USD",
                        CleaningFeeAmount = faker.Random.Decimal(25, 200),
                        CleaningFeeCurrency = "USD",
                        Amenities = new List<int> { },
                        LastBookedOn = DateTime.MinValue
                    });

                }

                const string sql = """
                INSERT INTO public.apartments
                (id, "name", description, address_country, address_state, address_zip_code, address_city, address_street, price_amount, price_currency, cleaning_fee_amount, cleaning_fee_currency, amenities, last_booked_on_utc)
                VALUES(@Id, @Name, @Description, @Country, @State, @ZipCode, @City, @Street, @PriceAmount, @PriceCurrency, @CleaningFeeAmount, @CleaningFeeCurrency, @Amenities, @LastBookedOn);
                """;
                connection.Execute(sql, apartments);
            }            
        }
    }
}
